// pages/perCenter/shipAddW/shipAddW.js
var util = require('../../../utils/util.js');
var dateUtil = require('../../../utils/data.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');
var app = getApp();
Page({
  data: {
    region: [],
    name: "",
    mobile: "",
    detail: "",
    isdefault: false,
    type: 'add',
    id: ''
  },

  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数


    this.data.region[0] = options.province
    this.data.region[1] = options.city
    this.data.region[2] = options.county
    var isdefault=false;
    if (options.isdefault=="true"){
      isdefault=true
    }else{
      isdefault=false
    }
    if (options.id) {
      this.setData({
        id: options.id,
        name: options.name,
        mobile: options.mobile,
        detail: options.detail,
        isdefault: isdefault,
        region: this.data.region,
        type: 'edit'
      })
    }
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },

  setName: function (e) {   //获取input输入的值
    var that = this;
    that.setData({
      name: e.detail.value
    })
  }, setmobile: function (e) {   //获取input输入的值
    var that = this;
    that.setData({
      mobile: e.detail.value
    })
  }, setAddress: function (e) {   //获取input输入的值
    var that = this;
    that.setData({
      detail: e.detail.value
    })
  }, setDefault: function (e) {   //获取input输入的值
    var that = this;
    if (that.data.isdefault == true) {
      that.setData({
        isdefault: false
      })
    } else {
      that.setData({
        isdefault: true
      })
    }

  },
  save: function () {
    var that = this;
    var obj = new Object();

    obj.id = that.data.id;
    obj.name = that.data.name;
    obj.mobile = that.data.mobile
    obj.province = that.data.region[0];
    obj.city = that.data.region[1];
    obj.county = that.data.region[2];
    obj.detail = that.data.detail;
    obj.isDefault = that.data.isdefault;
    let token = wx.getStorageSync('token');

    var mobileRules = /^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[8-9])[0-9]{8}$/;
    if (!mobileRules.test(that.data.mobile)) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon:'none'
      }) 
      return
    }
    if (that.data.name=="") {
      wx.showToast({
        title: '名称不能为空',
        icon: 'none'
      })
      return
    } if (that.data.name == "") {
      wx.showToast({
        title: '名称不能为空',
        icon: 'none'
      })
      return
    }


    wx.showLoading({
      title: '保存中',
    })
    util.request(api.AddrOper, { token: token, address: obj, type: that.data.type }).then(function (res) {
      wx.hideLoading();
      if (res.code === '200') {
        wx.navigateBack({

        })
      }
    });
  }
})



